package com.chuanyang.driverh.mainhome.fragment;


import android.content.Intent;
import android.view.View;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.base.OnCustomListener;
import com.chuanyang.driverh.mainhome.ChosedQualifyActivity;
import com.chuanyang.driverh.mainhome.NewQualifyActivity;
import com.chuanyang.driverh.mainhome.adapter.QualifyListAdapter;
import com.chuanyang.driverh.responseresult.QualifyData;
import com.chuanyang.driverh.responseresult.QualifyResult;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class QualifyFraggment extends BaseListFragment implements OnCustomListener {
    private int isPersonal = 1;
    private QualifyListAdapter adapter;
    private int type=1;//1最新 2已竞  3被选

    @Override
    public BaseListAdapter createAdapter() {
        return adapter=new QualifyListAdapter(getActivity());
    }

    @Override
    protected void initData() {
        getData();
    }

    @Override
    protected void initListener() {
        adapter.setOnCustomListener(this);
    }

    @Override
    public void onCustomerListener(View v, int position) {
        switch (v.getId())
        {
            case R.id.tv_delete:
                if (type==3){
                    PrintUtil.toast(getActivity(),"已被货主选择，无法删除！");
                    return;
                }

                QualifyData.DataBean  bean1= (QualifyData.DataBean) adapter.getItem(position);
                deleteAdvancedDriver(bean1.getAdvancedDriverId()+"",position);
                break;
            case R.id.ll_qualify_div:
                //// TODO: 2017/2/27
                QualifyData.DataBean  bean2= (QualifyData.DataBean) adapter.getItem(position);
                Intent intent=null;
                if (type==1){
                    intent=new Intent(getActivity(),NewQualifyActivity.class);
                }else{
                    intent=new Intent(getActivity(),ChosedQualifyActivity.class);
                }

                intent.putExtra("type",type);
                intent.putExtra("advancedDriverId",bean2.getAdvancedDriverId()+"");
                startActivity(intent);
                break;
        }

    }

    private void getData() {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("advancedType",isPersonal+"");
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("state",type+"");
        params.put("page", mCurrentPage + "");
        params.put("length", "10");
        MyHttpUtil.selectAdvancedOrderBydriver(getActivity(),params, new MyCallback<QualifyResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                springView.onFinishFreshAndLoad();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(QualifyResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {


                    try {
                        setDataResult(result.getData().getData());
                    } catch (Exception e) {
                        if (result.getMsg()!=null){
                            PrintUtil.toast(activityContext,result.getMsg());
                        }
                        adapter.clear();
                        adapter.notifyDataSetChanged();
                    }

                }else{
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }

    //删除预选单 已被货主选择的不可删除
    private void deleteAdvancedDriver(String id, final int position) {
        DialogUtil.showLoadingDialog(getActivity(),"删除...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("id", id);
        MyHttpUtil.deleteAdvancedDriver(getActivity(),params, new MyCallback<Result>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                e.printStackTrace();
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    PrintUtil.toast(getActivity(),"删除成功");
                    adapter.remove(position);
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });

    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    @Override
    public void onGetMore() {
        getData();
    }

    @Override
    protected void onForceRefresh() {
        mCurrentPage = 1;
        getData();
    }

    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getData();
    }


    public void refresh(int personal,int type){
        mCurrentPage = 1;
        this. isPersonal=personal;
        this.type=type;
        getData();
    }


}
